package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.HotelInfo;

public class HotelInfoLogic extends AbstractBL {
	public HotelInfo AddHotelInfo(long hotelId,String name,String address,String address2,String country,
			String state,String city,String zipcode,String phone,String fax,String logo,String photo,
			String website,String vatId,String description,String bAddress1,String bAddress2,
			String country2,String state2,String bCity,String bZipcode,String bPhone,String bFax,
			String cSal,String cName,String cLastname,String cDesign,String cPhone,String cExtn,
			String cFax,String cEmail,String cMobile,String email,String offered) throws EntityNotFoundException{
		HotelInfo a = new HotelInfo();
		a.setHotelId(hotelId);
		a.setAddress(address);
		a.setAddress2(address2);
		a.setBilladdress(bAddress1);
		a.setBilladdress2(bAddress2);
		a.setBillcity(bCity);
		a.setBillcountry(country2);
		a.setBillfax(bFax);
		a.setBillphone(bPhone);
		a.setBillstate(state2);
		a.setBillzipcode(bZipcode);
		a.setCity(city);
		a.setContactdesignation(cDesign);
		a.setContactemail(cEmail);
		a.setContactextn(cExtn);
		a.setContactfax(cFax);
		a.setContactfirstname(cName);
		a.setContactlastname(cLastname);
		a.setContactmobile(cMobile);
		a.setContactphone(cPhone);
		a.setContactsalutation(cSal);
		a.setCountry(country);
		a.setDescrition(description);
		a.setFax(fax);
		a.setLogo(logo);
		a.setName(name);
		a.setPhone(phone);
		a.setPhoto(photo);
		a.setState(state);
		a.setVatid(vatId);
		a.setWebsite(website);
		a.setZipcode(zipcode);
		a.setEmail(email);
		a.setOffered(offered);
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public HotelInfo EditHotelInfo(long id,String name,String address,String address2,String country,
			String state,String city,String zipcode,String phone,String fax,String logo,String photo,
			String website,String vatId,String description,String bAddress1,String bAddress2,
			String country2,String state2,String bCity,String bZipcode,String bPhone,String bFax,
			String cSal,String cName,String cLastname,String cDesign,String cPhone,String cExtn,
			String cFax,String cEmail,String cMobile,String email,String offered) throws EntityNotFoundException{	
		HotelInfo a = GetHotelInfo(id);
		a.setAddress(address);
		a.setAddress2(address2);
		a.setBilladdress(bAddress1);
		a.setBilladdress2(bAddress2);
		a.setBillcity(bCity);
		a.setBillcountry(country2);
		a.setBillfax(bFax);
		a.setBillphone(bPhone);
		a.setBillstate(state2);
		a.setBillzipcode(bZipcode);
		a.setCity(city);
		a.setContactdesignation(cDesign);
		a.setContactemail(cEmail);
		a.setContactextn(cExtn);
		a.setContactfax(cFax);
		a.setContactfirstname(cName);
		a.setContactlastname(cLastname);
		a.setContactmobile(cMobile);
		a.setContactphone(cPhone);
		a.setContactsalutation(cSal);
		a.setCountry(country);
		a.setDescrition(description);
		a.setFax(fax);
		a.setLogo(logo);
		a.setName(name);
		a.setPhone(phone);
		a.setPhoto(photo);
		a.setState(state);
		a.setVatid(vatId);
		a.setWebsite(website);
		a.setZipcode(zipcode);
		a.setEmail(email);
		a.setOffered(offered);
		
		Key	k=datastore.put(a.getEntity());
		Entity e = datastore.get(k);
		a = new HotelInfo();
		a.setEntity(e);

		return a;
	}
	
	public HotelInfo GetHotelInfo(long id)
	{
		Key key = KeyFactory.createKey(EntityName.HotelInfo, id);
		try {
			Entity lang = datastore.get(key);
			HotelInfo a = new HotelInfo();
			a.setEntity(lang);
			return a;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public HotelInfo GetHotelInfoByHotelId(long hotelId)
	{
		Query q = new Query(EntityName.HotelInfo);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);		
		
		q.setFilter(filterHotel);
		
		return GetHotelInfo(datastore.prepare(q).asSingleEntity());
	}
	
	public HotelInfo GetHotelInfo(Entity entity) {
		if(entity != null){
			HotelInfo item = new HotelInfo();
			item.setEntity(entity);
			return item;
		}else{
			return null;
		}
		
	}
	
	public List<HotelInfo> GetList(List<Entity> entities)
	{
		List<HotelInfo> list = new ArrayList<HotelInfo>();
		for(Entity e : entities)
		{
			HotelInfo a = new HotelInfo();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
}
